package com.ruoyi.cngj.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cngj.domain.CngjProject;
import com.ruoyi.cngj.service.ICngjProjectService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目案例Controller
 * 
 * @author yuexl
 * @date 2024-06-27
 */
@Controller
@RequestMapping("/cngj/project")
public class CngjProjectController extends BaseController
{
    private String prefix = "cngj/project";

    @Autowired
    private ICngjProjectService cngjProjectService;

    //@RequiresPermissions("cngj:project:view")
    @GetMapping()
    public String project()
    {
        return prefix + "/project";
    }

    /**
     * 查询项目案例列表
     */
    //@RequiresPermissions("cngj:project:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CngjProject cngjProject)
    {
        startPage();
        List<CngjProject> list = cngjProjectService.selectCngjProjectList(cngjProject);
        return getDataTable(list);
    }

    /**
     * 导出项目案例列表
     */
    //@RequiresPermissions("cngj:project:export")
    @Log(title = "项目案例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CngjProject cngjProject)
    {
        List<CngjProject> list = cngjProjectService.selectCngjProjectList(cngjProject);
        ExcelUtil<CngjProject> util = new ExcelUtil<CngjProject>(CngjProject.class);
        return util.exportExcel(list, "项目案例数据");
    }

    /**
     * 新增项目案例
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存项目案例
     */
    //@RequiresPermissions("cngj:project:add")
    @Log(title = "项目案例", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CngjProject cngjProject)
    {
        return toAjax(cngjProjectService.insertCngjProject(cngjProject));
    }

    /**
     * 修改项目案例
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CngjProject cngjProject = cngjProjectService.selectCngjProjectById(id);
        mmap.put("cngjProject", cngjProject);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目案例
     */
    //@RequiresPermissions("cngj:project:edit")
    @Log(title = "项目案例", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CngjProject cngjProject)
    {
        return toAjax(cngjProjectService.updateCngjProject(cngjProject));
    }

    /**
     * 删除项目案例
     */
    //@RequiresPermissions("cngj:project:remove")
    @Log(title = "项目案例", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cngjProjectService.deleteCngjProjectByIds(ids));
    }

    /**
     * 改变位置
     */
    @Transactional
    @PostMapping("/projectMove")
    @ResponseBody
    public AjaxResult projectMove(String type,String id,String changeId)
    {
        CngjProject cngjProject = cngjProjectService.selectCngjProjectById(id);
        CngjProject changeCngiProject = cngjProjectService.selectCngjProjectById(changeId);
        int num =0;
        if("1".equals(type) || "2".equals(type)){
            //上移
            int currSort = cngjProject.getProjectSort();
            cngjProject.setProjectSort(changeCngiProject.getProjectSort());
            changeCngiProject.setProjectSort(currSort);
            num +=cngjProjectService.updateCngjProject(cngjProject);
            num +=cngjProjectService.updateCngjProject(changeCngiProject);
        }
        if("3".equals(type)){
            cngjProject.setProjectSort(Integer.parseInt(changeId)+1);
            num +=cngjProjectService.updateCngjProject(cngjProject);
        }
        return toAjax(num);
    }

    /**
     * 启用停用banner素材
     */
    @Log(title = "启用停用项目案例素材")
    @PostMapping( "/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(String id,String projectState)
    {
        CngjProject cngjProject = new CngjProject();
        cngjProject.setId(id);
        cngjProject.setProjectState(projectState);
        return toAjax(cngjProjectService.updateCngjProject(cngjProject));
    }

}
